package top.carrotguo.weibo.model;

import android.text.TextUtils;

import com.orhanobut.logger.Logger;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import top.carrotguo.weibo.bean.HomeTimeLine;
import top.carrotguo.weibo.bean.UserInfo;
import top.carrotguo.weibo.bean.UserUidBean;
import top.carrotguo.weibo.contract.HomeContract;
import top.carrotguo.weibo.retrofit.RetrofitUtil;
import top.carrotguo.weibo.retrofit.api.AccountService;
import top.carrotguo.weibo.retrofit.api.StatusService;
import top.carrotguo.weibo.retrofit.callback.BaseCallback;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;
import top.carrotguo.weibo.utils.MyConstans;

public class HomeModel implements HomeContract.Model {
    public void getTimeline(Oauth2AccessToken token,int count,int page, final BaseCallback<HomeTimeLine,ErrorInfo> callback){
        //获取accessToken
        String accessToken = token.getToken();
        //动态加载类
        StatusService service = RetrofitUtil.getInstance().create(StatusService.class);
        Call<HomeTimeLine> call = service.getTimeline(accessToken,count,page);
        call.enqueue(new Callback<HomeTimeLine>() {
            @Override
            public void onResponse(Call<HomeTimeLine> call, Response<HomeTimeLine> response) {
                HomeTimeLine result = response.body();
                if (result == null || result.getStatuses() == null) {
                    callback.onError(new ErrorInfo(MyConstans.CALL_FAILURE,"加载数据为空"));
                } else {
                    callback.onSuccessed(result);
                }
            }

            @Override
            public void onFailure(Call<HomeTimeLine> call, Throwable t) {
                Logger.e(t.getMessage());
                callback.onError(new ErrorInfo(MyConstans.CALL_FAILURE,"客户端无法请求服务端"));
            }
        });
    }

    /**
     * 获取用户uid
     * @param accessToken
     * @param callback
     */
    @Override
    public void getUserUid(String accessToken, BaseCallback<String, ErrorInfo> callback) {
        AccountService service = RetrofitUtil.getInstance().create(AccountService.class);
        Call<UserUidBean> call = service.getUid(accessToken);
        call.enqueue(new Callback<UserUidBean>() {
            @Override
            public void onResponse(Call<UserUidBean> call, Response<UserUidBean> response) {
                UserUidBean successInfo = response.body();
                if (successInfo != null && TextUtils.isEmpty(successInfo.getError_code())) {
                    callback.onSuccessed(successInfo.getUid());
                } else if (!TextUtils.isEmpty(successInfo.getError_code())) {
                    callback.onError(new ErrorInfo(Integer.parseInt(successInfo.getError_code()),successInfo.getError()));
                } else {
                    callback.onError(new ErrorInfo(MyConstans.REQUEST_LIMIT,"接口访问受限"));
                }
            }

            @Override
            public void onFailure(Call<UserUidBean> call, Throwable t) {
                Logger.e(t.getMessage());
                callback.onError(new ErrorInfo(MyConstans.CALL_FAILURE,"请求接口有误"));
            }
        });
    }

    /**
     * 获取用户详情
     * @param access_token
     * @param uid
     * @param callback
     */
    @Override
    public void getUserInfo(String access_token, String uid, BaseCallback<UserInfo, ErrorInfo> callback) {
        AccountService service = RetrofitUtil.getInstance().create(AccountService.class);
        Call<UserInfo> call = service.getUserInfo(access_token,uid);
        call.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                UserInfo info = response.body();
                if (info != null) {
                    if (TextUtils.isEmpty(info.getError_code())) {
                        //请求成功
                        callback.onSuccessed(info);
                    } else {
                        callback.onError(new ErrorInfo(Integer.parseInt(info.getError_code()),info.getError()));
                    }
                } else {
                    callback.onError(new ErrorInfo(MyConstans.REQUEST_LIMIT,"请求接口受限"));
                }
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {
                Logger.e(t.getMessage());
                callback.onError(new ErrorInfo(MyConstans.CALL_FAILURE,"请求接口有误"));
            }
        });
    }
}
